package com.ruoyi.oos.tag.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.oos.tag.mapper.TagGroupMapper;
import com.ruoyi.oos.tag.domain.TagGroup;
import com.ruoyi.oos.tag.service.ITagGroupService;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.text.Convert;

/**
 * 测点分组Service业务层处理
 * 
 * @author wzy
 * @date 2020-05-07
 */
@Service
public class TagGroupServiceImpl implements ITagGroupService 
{
    @Autowired
    private TagGroupMapper tagGroupMapper;

    /**
     * 查询测点分组
     * 
     * @param id 测点分组ID
     * @return 测点分组
     */
    @Override
    public TagGroup selectTagGroupById(Long id)
    {
        return tagGroupMapper.selectTagGroupById(id);
    }

    /**
     * 查询测点分组列表
     * 
     * @param tagGroup 测点分组
     * @return 测点分组
     */
    @Override
    public List<TagGroup> selectTagGroupList(TagGroup tagGroup)
    {
        return tagGroupMapper.selectTagGroupList(tagGroup);
    }

    /**
     * 查询当前分组及所有子分组的ID列表
     * @param id
     * @return
     */
    public List<Long> selectSubTagGroupById(Long id) {
    	return tagGroupMapper.selectSubTagGroupById(id);
    }
    
    /**
     * 新增测点分组
     * 
     * @param tagGroup 测点分组
     * @return 结果
     */
    @Override
    public int insertTagGroup(TagGroup tagGroup)
    {
    	tagGroup.setType("0");
        return tagGroupMapper.insertTagGroup(tagGroup);
    }

    /**
     * 修改测点分组
     * 
     * @param tagGroup 测点分组
     * @return 结果
     */
    @Override
    public int updateTagGroup(TagGroup tagGroup)
    {
        return tagGroupMapper.updateTagGroup(tagGroup);
    }

    /**
     * 删除测点分组对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTagGroupByIds(String ids)
    {
        return tagGroupMapper.deleteTagGroupByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除测点分组信息
     * 
     * @param id 测点分组ID
     * @return 结果
     */
    public int deleteTagGroupById(Long id)
    {
        return tagGroupMapper.deleteTagGroupById(id);
    }

	@Override
	public List<Ztree> selectDeptTree(TagGroup tagGroup) {
		 List<TagGroup> deptList = tagGroupMapper.selectTagGroupList(tagGroup);
	        List<Ztree> ztrees = initZtree(deptList);
	        return ztrees;
	}
	
	  /**
     * 对象转部门树
     *
     * @param deptList 部门列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<TagGroup> deptList)
    {
        return initZtree(deptList, null);
    }

    /**
     * 对象转部门树
     *
     * @param deptList 部门列表
     * @param roleDeptList 角色已存在菜单列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<TagGroup> deptList, List<String> roleDeptList)
    {

        List<Ztree> ztrees = new ArrayList<Ztree>();
       // boolean isCheck = StringUtils.isNotNull(roleDeptList);
        for (TagGroup dept : deptList)
        {
            if (UserConstants.DEPT_NORMAL.equals(dept.getType()))
            {
                Ztree ztree = new Ztree();
                ztree.setId(dept.getId());
                ztree.setpId(dept.getPid());
                ztree.setName(dept.getGroupname());
                ztree.setTitle(dept.getGroupname());
                ztrees.add(ztree);
            }
        }
        return ztrees;
    }
	
}
